package cn.chrilwe.pigpig.user.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.google.common.base.Predicate;

import cn.chrilwe.pigpig.user.interceptor.RefreshTokenInterceptor;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 
 * @author chrilwe
 * 2020-8-6
 */
@Configuration
@EnableSwagger2
public class AppConfig implements WebMvcConfigurer {
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Bean
	public Docket docket() {
		Predicate<RequestHandler> p = RequestHandlerSelectors.basePackage("cn.chrilwe.pigpig.user");
		p.and(RequestHandlerSelectors.basePackage("cn.chrilwe.pigpig.core.model"));
		return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(p)
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("猪猪直播用户中心服务pigpig-user接口文档")
                        .description("用户服务接口文档")
                        .version("0.0.1")
                        .contact(new Contact("chrilwe","cn.chrilwe","chrilwe@qq.com"))
                        .license("pigpig-user 0.0.1")
                        .licenseUrl("http://localhost:8080")
                        .build());
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/user/**");
	}

}
